<template>
	<view class="main" :style="allStyle()">
		<view class="top" style="margin-top: 36px;margin-left: 16px;margin-right: 16px;">
			<u-button @click="jumpOne" text="海 绵 方 案 审 查 红 黑 榜" color="#FF5000" style="border-radius: 4px;" size="large"></u-button>
			<u-button @click="jumpTwo" text="调 度 指 令" color="#FF5000" style="border-radius: 4px;margin-top: 20px;"
				size="large" ></u-button>
			<u-button @click="jumpFour" text="项 目 巡 查" color="#FF5000" style="border-radius: 4px;margin-top: 20px;"
				size="large" ></u-button>
			<u-button @click="jumpSix" text="管 网 诊 断" color="#FF5000" style="border-radius: 4px;margin-top: 20px;"
				size="large" ></u-button>
			<u-button @click="jumpSeven" text="巡 查 日 志" color="#FF5000" style="border-radius: 4px;margin-top: 20px;"
				size="large" ></u-button>
			<u-button @click="jumpThree" text="问 题 上 报" color="#FF5000" style="border-radius: 4px;margin-top: 20px;"
				size="large"></u-button>
			<u-button @click="jumpFive" text="排 水 户 填 报" color="#FF5000" style="border-radius: 4px;margin-top: 20px;"
				size="large"></u-button>
			<u-button @click="jump8" text="项 目 巡 查 计 划" color="#FF5000" style="border-radius: 4px;margin-top: 20px;"
				size="large"></u-button>
		</view>
	</view>
</template>

<script>
	import croods from '@/common/croods-1.3.3.js';
	window.croods = croods;
	window.hydra = require('../../common/wst_hydra-1.0.0.js');


	import {
		getRoles,
		getUser,
	} from '@/common/api.js'
	import {
		mapState
	} from 'vuex';
	export default {
		computed: {
			...mapState(['loginStatus', 'isLogin', 'nextUpdateToken']),
		},
		data() {
			return {
				query: {
					roleCodes: ['xmzgzd', 'xmzgjb', 'xmzgsh']
				},
				btn1: false,
				btn2: false,
				btn3: false,
				btn4: false,
				btn5: false,
        btn8: false,
				role:null,
        timer: null,
			}
		},
		methods: {
			allStyle() {
				let height = uni.getSystemInfoSync().windowHeight
				return {
					'height': height,
					'width': '100%',
					'display': 'flex',
					'flex-direction': 'column',
					'background-color': '#FAFAFA'
				}
			},
			jumpSeven(){
				if(this.btn5){
					uni.navigateTo({
						url: '/pages/me/xcrz/index'
					})
				}else{
					uni.$u.toast('您当前登陆用户无相关使用权限，如需要请联系系统管理员。')
				}
			},
			jumpSix() {
				if(this.btn4){
					uni.navigateTo({
						url: '/pages/me/gwzd/list/index'
					})
				}else{
					uni.$u.toast('您当前登陆用户无相关使用权限，如需要请联系系统管理员。')
				}
			},
			jumpFive() {
				uni.navigateTo({
					url: '/pages/me/pshtb/index'
				})
			},
			jumpOne() {
				if(this.btn1){
					uni.navigateTo({
						url: '/pages/me/hhb/index'
					})
				}else{
					uni.$u.toast('您当前登陆用户无相关使用权限，如需要请联系系统管理员。')
				}
				
			},
			jumpTwo() {
				if(this.btn2){
					uni.navigateTo({
						url: '/pages/me/ddzl/index'
					})
				}else{
					uni.$u.toast('您当前登陆用户无相关使用权限，如需要请联系系统管理员。')
				}
			},
			jumpThree() {
				uni.navigateTo({
					url: '/pages/me/wtsb/index'
				})
			},
			jumpFour() {
				if(this.btn3){
					let res = this.role
					if ("msgCode" in res && res.msgCode == "200") {
						let id = 0
						uni.navigateTo({
							url: `/pages/me/xmxc/index?id=${id}`
						})
					} else {
						let id = 1
						let user = ''
						let zd = res.includes('xmzgzd')
						let jb = res.includes('xmzgjb')
						let sh = res.includes('xmzgsh')
						getUser().then(zoo => {
							user = zoo.userId
							uni.navigateTo({
								url: `/pages/me/xmxc/index?id=${id}&zd=${zd}&jb=${jb}&sh=${sh}&user=${user}`
							})
						})
					}
				}else{
					uni.$u.toast('您当前登陆用户无相关使用权限，如需要请联系系统管理员。')
				}
			},
      jump8() {
        if(this.btn8){
        	uni.navigateTo({
        		url: '/pages/me/xmxcjh/index'
        	})
        }else{
        	uni.$u.toast('您当前登陆用户无相关使用权限，如需要请联系系统管理员。')
        }
      },
			getRoleRes(){
				getRoles(this.query).then(res => {
					this.role = res
					if ("msgCode" in res && res.msgCode == "200") {
						this.btn5 = false
					} else {
						if(!(res.includes('xmzgzd') || res.includes('xmzgjb') || res.includes('xmzgsh') )){
							this.btn5 = false
						}
					}
				})
			},
      setRefreshTokenTimer(delay) {
        this.timer = setTimeout(() => {
          clearTimeout(this.timer)
          if(Date.now() > this.nextUpdateToken) {
            this.getRoleRes()
          }
          this.setRefreshTokenTimer()
        }, delay || 1000 * 30)
      },
		},
		mounted() {},
    onUnload() {
      clearTimeout(this.timer)
    },
    watch: {
      loginStatus(status) {
        if(status) {
          this.btn1 = true
          this.btn2 = true
          this.btn3 = true
          this.btn4 = true
          this.btn5 = true
          this.btn8 = true
          this.getRoleRes()
        }
      },
    },
		onShow() {
      this.setRefreshTokenTimer()
			document.body.style.overflow = 'auto'
			/* if(!this.isLogin){
				uni.reLaunch({
					url:'/pages/login/index'
				})
			} */
			if (this.loginStatus) {
				this.btn1 = true
				this.btn2 = true
				this.btn3 = true
				this.btn4 = true
				this.btn5 = true
        this.btn8 = true
				this.getRoleRes()
			}
		}
	}
</script>

<style>
	.main {
		background-color: #FAFAFA;
	}
</style>